local shendian = fk.CreateSkill{
  name = "xiaobai__shendian",
}

Fk:loadTranslationTable{
  ["xiaobai__shendian"] = "神点",
  [":xiaobai__shendian"] = "结束阶段，你可以重铸X次一张牌。发动期间本回合弃牌堆每有一种花色的牌数翻倍，"..
    "你便选择一次：1.摸一张牌；2.弃置一名角色一张牌（X为发动前本回合弃牌堆的牌数）。",

  ["#xiaobai__shendian"] = "神点：请重铸一张牌（第%arg次）",
  ["xiaobai__shendian_draw"] = "摸一张牌",
  ["xiaobai__shendian_discard"] = "弃置一名角色一张牌",
  ["#xiaobai__shendian_discard"] = "神点：请弃置% dest 的一张牌",
  ["@$xiaobai__shendian_additional-turn"] = "神点 发动时",
  ["@$xiaobai__shendian_origin-turn"] = "神点 弃牌堆",
}

shendian:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(shendian.name) and data.phase == Player.Finish and target == player
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local suits = {}
    local discards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insert(discards, info.cardId)
          end
        end
      end
    end, Player.HistoryTurn)

    for _, discard in ipairs(discards) do
      suits[Fk:getCardById(discard).suit] = (suits[Fk:getCardById(discard).suit] or 0) + 1
    end
    local num = math.min(#discards, 20)
    room:handleAddLoseSkills(player, "#xiaobai__shendian")
    for i = 1, num do
      if #player:getCardIds("he") == 0 then break end
      local cards = room:askToCards(player, {
        max_num = 1,
        min_num = 1,
        skill_name = shendian.name,
        include_equip = true,
        cancelable = false,
        prompt = "#xiaobai__shendian:::"..i,
      })
      if #cards > 0 then
        room:recastCard(cards, player, shendian.name)
        room:addPlayerMark(player, "xiaobai__shendian_draw")
      else
        break
      end
    end
    room:handleAddLoseSkills(player, "-#xiaobai__shendian")
    room:setPlayerMark(player, "xiaobai__shendian_draw", 1)
  end
})

local shendian_dealy = fk.CreateSkill{
  name = "#xiaobai__shendian"
}

shendian_dealy:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          room:addTableMarkIfNeed(player, "@$xiaobai__shendian_origin-turn", info.cardId)
        end
      end
    end
  end, Player.HistoryTurn)
end)

shendian_dealy:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    local skill_event = player.room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
    while skill_event and (skill_event.data.skill.skeleton.name ~= shendian.name 
      or skill_event.data.skill.skeleton.name ~= "xiaobai__ziyi") do
      skill_event = skill_event:findParent(GameEvent.SkillEffect)
    end
    if not (skill_event and player:hasSkill(shendian.name)) then
      return
    end
    local room = player.room
    local origin_id = player:getMark("@$xiaobai__shendian_origin-turn")
    local additional_id = player:getMark("@$xiaobai__shendian_additional-turn")
    local origin_suit = {}
    local additional_suit = {}
    for _, cid in ipairs(origin_id) do
      origin_suit[Fk:getCardById(cid).suit] = (origin_suit[Fk:getCardById(cid).suit] or 0) + 1
    end
    for _, cid in ipairs(additional_id) do
      additional_suit[Fk:getCardById(cid).suit] = (additional_suit[Fk:getCardById(cid).suit] or 0) + 1
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
            if table.contains(additional_id, info.cardId) then
              if additional_suit[Fk:getCardById(info.cardId).suit] == origin_suit[Fk:getCardById(info.cardId).suit] then
                return true
              end
            end
        end
      end
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local origin_id = player:getMark("@$xiaobai__shendian_origin-turn")
    local additional_id = player:getMark("@$xiaobai__shendian_additional-turn")
    local origin_suit = {}
    local additional_suit = {}
    for _, cid in ipairs(origin_id) do
      origin_suit[Fk:getCardById(cid).suit] = (origin_suit[Fk:getCardById(cid).suit] or 0) + 1
    end
    for _, cid in ipairs(additional_id) do
      additional_suit[Fk:getCardById(cid).suit] = (additional_suit[Fk:getCardById(cid).suit] or 0) + 1
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
            if table.contains(additional_id, info.cardId) then
              if additional_suit[Fk:getCardById(info.cardId).suit] == origin_suit[Fk:getCardById(info.cardId).suit] then
                self.doCost(self, event, target, player, data)
                additional_suit[Fk:getCardById(info.cardId).suit] = nil
              end
            end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {
      "xiaobai__shendian_draw",
      "xiaobai__shendian_discard"
    }
    local choices = {"xiaobai__shendian_draw"}
    if table.find(room.alive_players, function (p)
      return p ~= player and #p:getCardIds("he") > 0
    end) then
      table.insert(choices, "xiaobai__shendian_discard")
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      all_choices = all_choices,
      skill_name = shendian.name,
      cancelable = false,
    })
    if choice == "xiaobai__shendian_draw" then
      player:drawCards(1, shendian.name)
      room:addPlayerMark(player, "xiaobai__shendian_draw")
    elseif choice == "xiaobai__shendian_discard" then
      local target = room:askToChoosePlayers(player, {
        targets = table.filter(room.alive_players, function (p)
          return p ~= player and #p:getCardIds("he") > 0
        end),
        skill_name = shendian.name,
        max_num = 1,
        min_num = 1,
        cancelable = false,
        no_indicate = false
      })
      if #target > 0 then
        local c = room:askToChooseCard(player, {
          target = target[1],
          flag = "he",
          skill_name = shendian.name,
          cancelable = false,
          prompt = "#xiaobai__shendian_discard:::"..target[1].id,
        })
        room:throwCard(c, shendian.name, target[1], player)
      end
    end
  end,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(shendian.name)
  end,
  on_refresh =function (self, event, target, player, data)
    local skill_event = player.room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
    while skill_event and skill_event.data.skill.skeleton.name ~= shendian.name do
      skill_event = skill_event:findParent(GameEvent.SkillEffect)
    end
    local room = player.room
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if skill_event then
            room:addTableMarkIfNeed(player, "@$xiaobai__shendian_additional-turn", info.cardId)
          else
            room:addTableMarkIfNeed(player, "@$xiaobai__shendian_origin-turn", info.cardId)
          end
        end
      end
    end
  end
})

return {shendian, shendian_dealy}